﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AtletIS.Service.Helpers
{
    public static class PrintHelper
    {
        /// <summary>
        ///  Vráti dátum
        /// </summary>
        public static string PrintDate(DateTime dt)
        {
            if (dt.Day == 1 & dt.Month == 1 & dt.Year == 1900)
            {
                return string.Empty;
            }
            else if (dt.Day == 1 & dt.Month == 1)
            {
                return dt.Year.ToString();
            }
            else
            {
                return dt.ToString("dd.MM.yyyy");
            }
        }

        /// <summary>
        ///  Vráti výsledok podľa kódu disciplíny
        /// </summary>
        public static string PrintResult(string result, int disciplineCode)
        {
            string strResult;

            if (result == "0")
            {
                return string.Empty;
            }

            switch (disciplineCode)
            {
                case Constants.MainConstants.SPRINT_CODE:
                case Constants.MainConstants.LONG_RUN_CODE:
                    strResult = result.PadLeft(8, '0');
                    strResult = strResult.Substring(0, 2) + ":" + strResult.Substring(2, 2) + ":" + strResult.Substring(4, 2) + "," + strResult.Substring(6, 2);
                    strResult = strResult.TrimStart('0', ':');
                    return strResult;

                case Constants.MainConstants.THROWS_CODE:
                case Constants.MainConstants.HOR_JUMP_CODE:
                    if (result == "x" | result == "-")
                        strResult = result;
                    else
                    {
                        strResult = result.PadLeft(4, '0');
                        strResult = strResult.Substring(0, 2) + "," + strResult.Substring(2, 2);
                        strResult = strResult.TrimStart('0');
                    }
                    return strResult;

                case Constants.MainConstants.VER_JUMP_CODE:
                    return result;

                case Constants.MainConstants.MARATHON_CODE:
                    result = RoundSecondsUp(result);
                    strResult = result.PadLeft(8, '0');
                    strResult = strResult.Substring(0, 2) + ":" + strResult.Substring(2, 2) + ":" + strResult.Substring(4, 2);
                    strResult = strResult.TrimStart('0', ':');
                    return strResult;

                default:
                    return string.Empty;
            }
        }

        /// <summary>
        ///  Vráti čas zaokrúhlený na sekundy nahor
        /// </summary>
        private static string RoundSecondsUp(string sResult)
        {
            TimeSpan tsMarathonResult = TimeSpanHelper.FromString(sResult);

            if (tsMarathonResult.Milliseconds == 0)
            {
                return string.Format("{0}{1:00}{2:00}00", tsMarathonResult.Hours, tsMarathonResult.Minutes, tsMarathonResult.Seconds);
            }
            else if (tsMarathonResult.Seconds == 59)
            {
                return string.Format("{0}{1:00}{2:00}00", tsMarathonResult.Hours, tsMarathonResult.Minutes + 1, 0);
            }
            else
            {
                return string.Format("{0}{1:00}{2:00}00", tsMarathonResult.Hours, tsMarathonResult.Minutes, tsMarathonResult.Seconds + 1);
            }
        }




    }
}
